package com.whp.yky.yky.service;


import com.whp.yky.yky.entity.poentity.Document;

import com.whp.yky.yky.mapper.DocumentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DocumentService {


    @Autowired
    private DocumentMapper documentMapper;


    public List<Document> getDocumentInfoByDocumentName(String document_name){
        return documentMapper.getDocumentInfoByDocumentName(document_name);
    }

    public List<Document> findAll(){
        return documentMapper.findAll();
    }

    public boolean insertDocumentInfo(Document document){

        if (document.getId()!=null&&!"".equals(document.getId())){

            try {
                int effecteNum= documentMapper.insertDocumentInfo(document);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    System.out.println("增加成功，主键为："+document.getId());
                    return true;
                }else {
                    throw  new RuntimeException("插入信息失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("插入信息失败了:"+e.getMessage());
            }
        }else{
            throw new RuntimeException("信息不能为空！！！！");
        }

    }

    public boolean updateDocumentInfo(Document document){

        if (document.getId()!=null&&!"".equals(document.getId())){

            try {
                int effecteNum= documentMapper.updateDocumentInfo(document);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    System.out.println("修改成功，主键为："+document.getId());
                    return true;
                }else {
                    throw  new RuntimeException("修改失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("修改失败:"+e.getMessage());
            }
        }else{
            throw new RuntimeException("信息不能为空！！！！");
        }

    }


    public boolean deleteDocumentById(String id) {

        try {
            //对mysql数据库做删除操作后，影响的行数effecteNum
            int effecteNum= documentMapper.deleteDocumentById(id);
            if (effecteNum>0){
                return true;
            }else {
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }



}
